<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="主键id" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入主键id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['integral:integral:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['integral:integral:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['integral:integral:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['integral:integral:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="integralList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="商品图片" align="center" prop="productImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.productImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="活动标题" align="center" prop="eventTitle" />
      <el-table-column label="兑换积分" align="center" prop="redeemPoints" />
      <el-table-column label="限量" align="center" prop="limited" />
      <el-table-column label="限量剩余" align="center" prop="limitedRemainder" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['integral:integral:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['integral:integral:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改积分商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-steps :active="active" finish-status="success">
        <el-step title="步骤 1"></el-step>
        <el-step title="步骤 2"></el-step>
        <el-step title="步骤 3"></el-step>
      </el-steps>
        <div v-show="active==0||active==1" v-if="title=='添加积分商品'">
          <el-form-item label="选择商品">
            <div @click="handleSpxx()">
              <image-upload style="pointer-events: none" v-model="imageShow"></image-upload>
            </div>
          </el-form-item>
        </div>
<!--        <el-button @click="handleSpxx" icon="el-icon-goods" center v-if="active==0" :width="100" :height="100"></el-button>-->
<!--        <image-preview :src="imageShow" :width="100" :height="100" v-if="this.imageShow!=null&&active==0"/>-->
        <el-dialog
          append-to-body
          title="提示"
          :visible.sync="dialogVisible"
          width="30%"
          :before-close="handleClose">
          <template>
            <el-table
              :data="tableData"
              stripe
              style="width: 100%"
              @selection-change="handleSelectionProduct">
              <el-table-column
                type="selection"
                width="55">
              </el-table-column>
              <el-table-column
                prop="id"
                label="商品id"
                width="180">
              </el-table-column>
              <el-table-column
                prop="image"
                label="商品图片"
                width="180">
                <template slot-scope="scope">
                  <image-preview :src="scope.row.image" :width="50" :height="50"/>
                </template>
              </el-table-column>
              <el-table-column
                prop="productName"
                label="商品信息">
              </el-table-column>
            </el-table>
          </template>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="setFrom">确 定</el-button>
        </span>
        </el-dialog>
        <el-form-item label="商品图片" prop="productImage" v-if="active==1">
          <image-upload v-model="form.productImage"/>
        </el-form-item>
        <el-form-item label="商品名称" prop="eventTitle" v-if="active==1">
          <el-input v-model="form.eventTitle" placeholder="请输入活动标题" />
        </el-form-item>
        <el-form-item label="用户兑换数量限制" prop="exchangeRemainder" v-if="active==1">
          <el-input v-model="form.exchangeRemainder" placeholder="请输入用户兑换数量限制" />
        </el-form-item>
        <el-form-item label="单位" prop="unit" v-if="active==1">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="排序" prop="sort" v-if="active==1">
          <el-input v-model="form.sort" placeholder="请输入排序" v-if="active==1" />
        </el-form-item>
        <el-form-item label="上架状态" prop="productStatus" v-if="active==1">
          <el-radio-group v-model="form.productStatus">
            <el-radio :label="1">开启</el-radio>
            <el-radio :label="2">关闭</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="热门推荐" prop="recommendation" v-if="active==1">
          <el-radio-group v-model="form.recommendation">
            <el-radio :label="1">开启</el-radio>
            <el-radio :label="2">关闭</el-radio>
          </el-radio-group>
        </el-form-item>
        <template v-if="this.active==1">
          <el-table
            :data="tableDataGg"
            stripe
            style="width: 100%"
            @selection-change="handleSelectionProductGg">
            <el-table-column
              type="selection"
              width="55">
            </el-table-column>
            <el-table-column label="尺码" align="center" prop="cm" />
            <el-table-column label="颜色分类" align="center" prop="color" />
            <el-table-column label="图片" align="center" prop="tp" >
              <template slot-scope="scope">
                <image-preview :src="scope.row.tp" :width="50" :height="50"/>
              </template>
            </el-table-column>
            <el-table-column label="兑换积分" align="center" prop="dhjf" />
            <el-table-column label="库存" align="center" prop="kc" />
            <el-table-column label="限量" align="center" prop="xl" />
            <el-table-column label="重量" align="center" prop="zl" />
            <el-table-column label="体积" align="center" prop="tj" />
          </el-table>
        </template>
          <div v-if="this.active==4">
            <el-form-item label="限量" prop="limited" v-if="active==1">
              <el-input v-model="form.limited" placeholder="请输入限量" />
            </el-form-item>
            <el-form-item label="兑换积分" prop="redeemPoints" v-if="active==1">
              <el-input v-model="form.redeemPoints" placeholder="请输入兑换积分" />
            </el-form-item>
            <el-form-item label="限量剩余" prop="limitedRemainder" v-if="active==1">
              <el-input v-model="form.limitedRemainder" placeholder="请输入限量剩余" />
            </el-form-item>
            <el-form-item label="规格尺码" prop="specSize" v-if="active==1">
              <el-input v-model="form.specSize" placeholder="请输入规格尺码" />
            </el-form-item>
            <el-form-item label="颜色分类" prop="color" v-if="active==1">
              <el-input v-model="form.color" placeholder="请输入颜色分类" />
            </el-form-item>
            <el-form-item label="规格图片" prop="specImage" v-if="active==1">
              <image-upload v-model="form.specImage"/>
            </el-form-item>
            <el-form-item label="库存" prop="inventory" v-if="active==1">
              <el-input v-model="form.inventory" placeholder="请输入库存" />
            </el-form-item>
            <el-form-item label="体积" prop="volume" v-if="active==1">
              <el-input v-model="form.volume" placeholder="请输入体积" />
            </el-form-item>
          </div>
        <el-form-item label="详情"  v-if="active==2">
          <editor v-model="form.detail" :min-height="192" v-html="form.detail"/>
        </el-form-item>
      </el-form>
      <el-button style="margin-top: 12px;" @click="previous">上一步</el-button>
      <el-button style="margin-top: 12px;" @click="next">下一步</el-button>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="active==2">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listIntegral, getIntegral, delIntegral, addIntegral, updateIntegral ,ggList} from "@/api/integral/integral";
import { listComments, getComments, delComments, addComments, updateComments,productList } from "@/api/comments/comments";
export default {
  name: "Integral",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      dialogVisible:false,
      imageShow:null,
      tableDataGg:[],
      // 非多个禁用
      multiple: true,
      active: 0,
      tableData:[],
      // 显示搜索条件
      showSearch: true,
      multipleSelection:[],
      // 总条数
      total: 0,
      // 积分商品表格数据
      integralList: [],

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 详情时间范围
      daterangeCreateTime: [],
      listGg:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        createTime: null,
        productStatus: null,
      },
      // 表单参数
      form: {},
      formGg:{
        cm: null,
        color: null,
        tp: null,
        dhjf: null,
        kc: null,
        xl: null,
        zl: null,
        tj: null,
        jf:null
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getSelectProduct();
    this.selectGg();
  },
  methods: {
    setGg(val){
      this.formGg=val;
    },
    selectGg(){
      ggList(null).then(res=>{
        this.tableDataGg=res;
      })
    },
    handleSpxx(){
      this.dialogVisible=true;
    },
    handleSelectionProductGg(val){
      this.listGg = val
      try {
        // 确保 this.listGg 是数组且不为空
        if (Array.isArray(this.listGg) && this.listGg.length > 0) {
          console.log(this.listGg[0]); // 输出数组中的第一个元素
        } else {
          console.log("this.listGg is undefined or empty.");
        }
      } catch (error) {
        console.error("Error accessing properties:", error);
      }
      this.form.redeemPoints=this.listGg[0].dhjf;
      this.form.limited=this.listGg[0].xl;
      this.form.limitedRemainder=this.listGg[0].xl;
      this.form.status=this.form.productStatus;
      console.log(val)
    },
    setFrom(){
      this.form.productId=this.multipleSelection[0].id;
      this.form.productImage=this.multipleSelection[0].image;
      this.form.eventTitle=this.multipleSelection[0].productName;
      this.form.unit=this.multipleSelection[0].unit;
      this.form.sort=this.multipleSelection[0].sort;
      this.form.productStatus=this.multipleSelection[0].status;
      this.form.detail='<img src="'+this.multipleSelection[0].image+'">';
      this.imageShow=this.multipleSelection[0].image;
      this.dialogVisible=false;
    },
    removeBackslashes(str) {
      // 使用 replace 方法去除所有的反斜杠
      return str.replace(/<[^>]*>/g, '');
    },
    getSelectProduct(){
      productList().then(res=>{
        this.tableData=res;
      })
    },
    handleSelectionProduct(val){
      this.multipleSelection = val
      if (val.length > 1) {
        this.$refs.multipleTableFrag.clearSelection()
        this.$refs.multipleTableFrag.toggleRowSelection(val.pop())
      }
      console.log(val[0]['name'])
    },
    handleClose(){
      this.$confirm('确认关闭？')
        .then(_ => {
          this.dialogVisible=false
        })
        .catch(_ => {});
    },
    previous(){
      this.active--;
    },
    next() {
      if (this.active++ > 2) this.active = 0;
    },
    /** 查询积分商品列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listIntegral(this.queryParams).then(response => {
        this.integralList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        productImage: null,
        eventTitle: null,
        redeemPoints: null,
        limited: null,
        limitedRemainder: null,
        createTime: null,
        sort: null,
        status: null,
        productId: null,
        exchangeRemainder: null,
        unit: null,
        productStatus: null,
        recommendation: null,
        specSize: null,
        color: null,
        specImage: null,
        inventory: null,
        volume: null,
        detail: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加积分商品";
      this.active=0;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.active=1;
      this.reset();
      const id = row.id || this.ids
      getIntegral(id).then(response => {
        this.form = response.data;
        this.form.detail='<img src="'+this.form.productImage+'">';
        this.open = true;
        this.title = "修改积分商品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.form.detail=this.removeBackslashes(this.form.detail)
            updateIntegral(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.detail=this.form.image;
            addIntegral(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除积分商品编号为"' + ids + '"的数据项？').then(function() {
        return delIntegral(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('integral/integral/export', {
        ...this.queryParams
      }, `integral_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
